package cn.tedu.blog.api.service.impl;

import cn.tedu.blog.api.ex.ServiceException;
import cn.tedu.blog.api.mapper.ArticleMapper;
import cn.tedu.blog.api.pojo.restful.JsonPage;
import cn.tedu.blog.api.pojo.vo.ArticleStandardVO;
import cn.tedu.blog.api.repo.IArticleRepository;
import cn.tedu.blog.api.service.IArticleService;
import cn.tedu.blog.api.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

import static cn.tedu.blog.api.repo.IArticleRepository.*;


@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private IArticleRepository articleRepository;
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 最新文章查询
     * @return
     */
    @Override
    public List<ArticleStandardVO> selectNewestArticle() {
        log.debug("====执行service查询方法====");
       boolean isExpired= articleRepository.isExpired(KEY_NEWEST_ARTICLE_LIST);
        if(isExpired){
            log.debug("====执行数据库查询存入redis====");
            List<ArticleStandardVO> list =articleMapper.selectNewest();
            if(list==null){
             throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据库中没有文章");
            }
            articleRepository.putList(list,KEY_NEWEST_ARTICLE_LIST);
        }
        List<ArticleStandardVO> articleStandardVOS =articleRepository.getList(KEY_NEWEST_ARTICLE_LIST);

        return articleStandardVOS;
    }

    /**
     * 热门文章查询
     * @return
     */
    @Override
    public List<ArticleStandardVO> selectPopularArticle() {
        boolean isExpired= articleRepository.isExpired(KEY_POPULAR_ARTICLE_LIST);
        if(isExpired){
            log.debug("====执行数据库查询存入redis====");
            List<ArticleStandardVO> list =articleMapper.selectPopularArticle();
            if(list==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据库中没有文章");
            }
            articleRepository.putList(list,KEY_POPULAR_ARTICLE_LIST);
        }
        List<ArticleStandardVO> articleStandardVOS = articleRepository.getList(KEY_POPULAR_ARTICLE_LIST);

        return articleStandardVOS;


    }

    /**
     * 管理员推荐文章查询
     * @return
     */
    @Override
    public List<ArticleStandardVO> selectIsRecommendArticle() {
        boolean isExpired= articleRepository.isExpired(KEY_IS_RECOMMEND_ARTICLE_LIST);
        if(isExpired){
            log.debug("====执行数据库查询存入redis====");
            List<ArticleStandardVO> list =articleMapper.selectIsRecommendArticle();
            if(list==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据库中没有文章");
            }
            articleRepository.putList(list,KEY_IS_RECOMMEND_ARTICLE_LIST);
        }
        List<ArticleStandardVO> articleStandardVOS = articleRepository.getList(KEY_IS_RECOMMEND_ARTICLE_LIST);

        return articleStandardVOS;

    }

    /*分页查询文章列表*/
    @Override
    public JsonPage<ArticleStandardVO> selectArticleLists(Integer pageNum, Integer pageSize){
             PageHelper.startPage(pageNum,pageSize);
            log.debug("====执行数据库查询====");
            List<ArticleStandardVO> list =articleMapper.selectIsArticlePageList();
            if(list==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据库中没有文章");
            }
             return JsonPage.restPage(new PageInfo<>(list));
    }
    /*查询文章数量*/
    @Override
    public Long selectArticleCount() {
        Long count =articleMapper.selectArticleCount();
        return count;
    }
    /**/


}
